package com.hongdee.crm.wo.controller;

import com.hongdee.base.entity.ResultEntity;
import com.hongdee.common.util.EntityUtils;
import com.hongdee.common.util.TokenUtil;
import com.hongdee.crm.wo.entity.TCrmWoType;
import com.hongdee.crm.wo.entity.TCrmWoXfType;
import com.hongdee.crm.wo.service.ITCrmWoService;
import com.hongdee.crm.wo.service.ITCrmWoXfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Created by dell on 2017/5/10.
 */
@RestController
@RequestMapping("/crm/wo")
public class TCrmWoXfController {
    @Autowired
    private ITCrmWoXfService itCrmWoXfService;
    @RequestMapping("/xftype/save")
    @ResponseBody
    public ResultEntity save(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody TCrmWoXfType tCrmWoXfType) throws Exception {
        EntityUtils.valid(tCrmWoXfType);
        System.out.println(tCrmWoXfType);
        TCrmWoXfType group = itCrmWoXfService.saveOrUpdate(tCrmWoXfType,uid);
        ResultEntity res = new ResultEntity();
        res.setData(group);
        res.setSuccess(true);
        res.setMsg("保存成功！");
        System.out.println("这是我返回的东西11111111111111111111111111111111111111111111111111"+res);
        return res;
    }
    /*
    * 查询类别详情接口
    * */
    @RequestMapping("/xftype/info")
    @ResponseBody
    public ResultEntity info(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        EntityUtils.valid(id);
        ResultEntity res = new ResultEntity();
        Map map = itCrmWoXfService.info(id);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }
    /**
     * 停用工单类别对应接口
     */
    @RequestMapping("/xftype/stop")
    @ResponseBody
    public ResultEntity stop(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "0") String id) throws Exception {
        EntityUtils.valid(id);
        ResultEntity res = new ResultEntity();
        itCrmWoXfService.update(id, "0");
        res.setSuccess(true);
        res.setMsg("停用成功！");
        return res;
    }
    /**
     * 启用维系类别接口
     */
    @RequestMapping("/xftype/start")
    @ResponseBody
    public ResultEntity start(@RequestParam(name = "uid", defaultValue = "") String uid,
                              @RequestParam(name = "token", defaultValue = "") String token,
                              @RequestParam(name = "id", defaultValue = "0") String id) throws Exception {
        EntityUtils.valid(id);
        ResultEntity res = new ResultEntity();
        itCrmWoXfService.update(id, "1");
        res.setSuccess(true);
        res.setMsg("启动成功！");
        return res;
    }
    /*
    * 分页
    * */
    @RequestMapping("/xftype/page")
    @ResponseBody
    public Map newPage(@RequestParam(name = "uid", defaultValue = "") String uid,
                       @RequestParam(name = "token", defaultValue = "") String token,
                       @RequestParam(name = "start", defaultValue = "0") String start,
                       @RequestParam(name = "limit", defaultValue = "50") String limit
    ) throws Exception {
        /*
        * 获取公司id
        * */
        String oid= TokenUtil.getCompanyId(uid);
        Map map = this.itCrmWoXfService.newPage(start, limit,oid);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }
}
